package com.jinxin.platform.cdinfogo.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-11-29
 * @Author: Lingnan
 **/
public class DateUtil {

    /**
     * 根据时间 和时间格式 校验是否正确
     * @param length 校验的长度
     * @param sDate 校验的日期
     * @param format 校验的格式
     * @return
     */
    public static boolean isLegalDate(int length, String sDate,String format) {
        if ((sDate == null) || (sDate.length() != length)) {
            return false;
        }
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        String s1 = "10:11:29";
        System.out.println(isLegalDate(s1.length(),s1,"HH:mm:ss"));
//        String s1 = "2021-11-29";
//        String s2 = "2021-11-29 14:36:30";
//        System.out.println(isLegalDate(s1.length(),s1,"yyyy-MM-dd"));
//        System.out.println(isLegalDate(s2.length(),s2,"yyyy-MM-dd HH:mm:ss"));
    }
}
